import os
import gc
import time
from multiprocessing import Process, Manager
import flask
from flask import Flask, request,json
from flask_cors import *
import cv2
import uuid
from threading import Thread
from time import sleep
from flask_cors import CORS

app = Flask(__name__)

def playWay(path,id):
    cap = cv2.VideoCapture(path)
    ret, frame = cap.read()
    while ret:
        ret, frame = cap.read();
        cv2.imwrite('./static/img/videoPlay.jpg', frame)  # 存储为图像
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cv2.destroyAllWindows()
    cap.release()


def asyncWay(f):
    def wrapper(*args, **kwargs):
        thr = Thread(target=f, args=args, kwargs=kwargs)
        thr.start()
    return wrapper


@asyncWay
def asyncVideo(path,id):
    sleep(0.001)
    playWay(path,id);

@app.route("/video", methods=['get'])
def video():
    path = request.args.to_dict().get('url')
    id = uuid.uuid1();
    asyncVideo(path,id);
    return json.dumps({"code": 200, "url":id ,"type":"jpg", "msg": "请求视频成功"});

@app.route('/')
def hello_world():
    #readImg()
    return flask.render_template('index.html')



if __name__ == "__main__":
	CORS(app, supports_credentials=True)
	app.run(debug=False, host='0.0.0.0', port=5000)


